using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace WEATHER.Components
{
	public partial class Item
	{
        //public static Guid GetModuleID(string weatherType)
        //{
        //    const string spName = "[dbo].[p_WEATHER_Item_GetModuleIDBy_WeatherType]";
        //    SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
        //    SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
        //    db.AddInParameter(dbCommand, "@WeatherType", SqlDbType.VarChar, weatherType);
        //    db.AddOutParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, 16);

        //    db.ExecuteNonQuery(dbCommand);
        //    return new Guid(db.GetParameterValue(dbCommand, "@ModuleID").ToString());

        //}

	    public static DataSet SelectTopBy_WeatherType(Guid moduleID, string weatherType, int top)
        {
            const string spName = "[dbo].[p_WEATHER_Item_SelectTopBy_WeatherType]";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@WeatherType", SqlDbType.VarChar, weatherType);
            db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            db.AddInParameter(dbCommand, "@Top", SqlDbType.Int, top);

            return db.ExecuteDataSet(dbCommand);
        }

        public static int MaxOfID(string weatherType)
        {
            int max = -1;
            try
            {
                const string spName = "[dbo].[p_WEATHER_Item_MaxOfID]";
                SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
                SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
                db.AddOutParameter(dbCommand, "@ID", SqlDbType.Int, 4);                
                db.AddInParameter(dbCommand, "@WeatherType", SqlDbType.VarChar, weatherType);

                db.ExecuteNonQuery(dbCommand);
                max = (int)db.GetParameterValue(dbCommand, "@ID");
            }
            catch (Exception){}
            return max;
        }
	}	
}